/**
 * 所有的请求统一通过这里，然后请求后台
 */
var port_json = {
    // "addr": "http://192.168.1.143:8080/api",
    // "cdnaddr": "https://file.worldwz.com",

    // "addr": "https://weizhen1.1000ask.com/api",
    // "cdnaddr": "https://file.worldwz.com",

    // "addr": "https://wz.1000ask.com/api", //克隆版本测试环境 
    // "cdnaddr": "https://wzfile.1000ask.com/file",

    "cdnaddr": "https://worldwz.oss-cn-beijing.aliyuncs.com/xcx/image",
    "appaddr": "https://merchant-test.worldwz.com/app",//APP测试环境
    "addr": "https://xcx-test.worldwz.com/xcx",//测试环境
    "imAddr": "https://im-test.worldwz.com/im",//im测试环境

    // "appaddr": "https://merchant.worldwz.com/app",//APP生产环境
    // "imAddr": "https://im.worldwz.com/im",//im正式环境
    // "addr": "https://xcx.worldwz.com/xcx",//正式生产环境
    //"addr": "http://172.16.20.170:8081/xcx",//本机测试环境
    //"appaddr": "http://192.168.1.147:80/app",
    //"addr": "http://test-wz.natapp1.cc/xcx",
};

/**
 * 获取图片前缀域名,支持传入参数进行获取对应的地址
 */
function get_image_prefix_host(image_alias = '') {
    if (image_alias == '') {
        return port_json['cdnaddr'];
    } else {
        return port_json[image_alias];
    }
}


/**
 * 传入接口名，返回请求地址
 * portname 接口名 string
 * app getApp() 全局app
 * upload_data 参数 object
 * callback 回调函数 function 
 */
function getPort(portname, app = {}, upload_data = {}, success_callback = '', fail_callback = '', complete_callback = '') {
    // var uid = app.globalData.userId
    // var token = app.globalData.token
    var requset_data = {};
    var requset_config = {};
    if (upload_data.method) {
        requset_config.method = upload_data.method;
    } else {
        requset_config.method = 'POST';
    }
    if (upload_data.dataType) {
        requset_config.dataType = upload_data.dataType;
    } else {
        requset_config.dataType = 'json';
    }
    for (var key in upload_data) {
        if (key !== 'method' && key !== 'dataType' && key !== 'verifyResult') {
            requset_data[key] = upload_data[key];
        }
    }
    var requset_url = port_json['addr'] + port_json[portname];
    if (!requset_url) {
        console.log('没有找到对应接口地址，检查参数');
    } else {
        requset_config.url = requset_url;
        requset_config.data = requset_data;
        requset_config.success = function(res) {
            //对于json字符串进行解析，后台传递过来的值有可能是字符串
            try {
                if (typeof JSON.parse(res.data) == "object") {
                    console.warn('----->后台返回的数据为字符串，自动转换=>', res);
                    var jsonData = JSON.parse(res.data);
                    res.data = jsonData;
                }
            } catch (e) {

            }

            if (upload_data.verifyResult !== '' && upload_data.verifyResult !== undefined) {
                if (res.data.code !== 200 && res.data.code !== undefined) {
                    wx.showToast({
                        title: res.data.message ? res.data.message : '网络错误,检查网络',
                        mask: true,
                        icon: 'none',
                        duration: 2000
                    });
                    return false;
                }
            }
            console.group(portname);
            console.log(portname + '接口请求参数' + '=>', requset_data);
            console.log(portname + '返回参数' + '=>', res);
            console.groupEnd();

            if (success_callback != '') {
                success_callback(res);
            }
        };
        requset_config.fail = function(res) {
            if (fail_callback != '') {
                fail_callback(res);
            }
        };
        requset_config.fail = function(res) {
            if (complete_callback != '') {
                complete_callback(res);
            }
        };
        wx.request(requset_config)
    }
}

/**
 * 上传图片
 * portname 接口名 string
 * success_call 上传成功后的回调函数
 * fail_call 上传失败后的回调函数
 */
function uploadImg(portname, app = {}, success_call = "", fail_call = "") {
    var requset_url = port_json['host'] + port_json[portname];

    var uid = app.globalData.userId
    var token = app.globalData.token
    var data = {
        uid: uid,
        token: token
    }
    wx.chooseImage({
        count: 1, // 默认9  
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有  
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
        success: function(res) {
            var tempFilePaths = res.tempFilePaths
            wx.uploadFile({
                url: requset_url, //仅为示例，非真实的接口地址
                filePath: tempFilePaths[0],
                name: 'file',
                formData: data,
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                success: function(res) {
                    if (success_call != "") {
                        success_call(res);
                    }
                },
                fail: function(res) {
                    if (fail_call != "") {
                        fail_call(res);
                    }
                },
            })
        }
    })
}

/**
 * 封装微信请求
 * @param config	所有的请求参数
 */
function request(config) {
    config.method = 'POST';
    wx.request(config);
}

module.exports = {
    getPort: getPort,
    uploadImg: uploadImg,
    getImagePrefixHost: get_image_prefix_host,
    request: request
}